<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	基本调用
// +----------------------------------------------------------------------*/
namespace app\Admin\controller;
use think\Controller;
use Casbin;
use think\Db;

class Base extends Controller {
	protected $module;
	protected $controller;
	protected $action;
	protected $user_info;
	public $reJson;
    protected function initialize(){
			$this->reJson= ['code'=>0,'msg'=>'操作失败','data'=>''];
			$this->checkStatus();

			$this->user_info = session('user_info');
			$this->module = request()->module();
			$this->controller = request()->controller();
			$this->action = request()->action();
			$this->assign('user_info',$this->user_info);
			$this->assign('basic',$this->getBasic());
			$this->assign('menu',getTree($this->amMenu(),'child'));
			$this->assign('toolUrl',$this->controller);
				$url = strtolower($this->module . '/' . $this->controller . '/' . $this->action);
				if (Casbin::enforce($this->user_info['id'],$url) || $this->checkAmEdit()) {
					return true;
				}else{
					//echo "{'code':404,'msg':'没有权限!'}";
					header('Content-Type:text/html; charset=utf-8');
					$this->error('没有权限,请联系管理员!','','reload','400');
				}
	}
	/*基本系统信息*/
	protected function getBasic(){
		$res = Db::name('basic')->find();
		return $res;
	}
	/*获取菜单*/
	protected function amMenu(){
		$res = Db::name('node')->where(['is_menu'=>1,'module_name'=>'admin'])->select();
		return $res;
	}
	/*查看登陆状态,登陆是否超时*/
	protected function checkStatus(){
		if(session('?user_info')){
			$ltime = app('session')->get('user_info.login_time') + config('session.expire');
				if($ltime < time()){
					session(null);
					header('Content-Type:text/html; charset=utf-8');
					$this->error('登陆超时,请刷新后重新登陆!',url('login/index'),'reload','029');
				}else{
					app('session')->set('user_info.login_time',time());
				}			
		}else{
			$this->redirect(url('login/index'));
		}

	}
	/*排除自己编辑自己*/
	public function checkAmEdit(){
		$path = request()->path();
		$id = request()->param('id');
		if($this->user_info['id']  ==  $id && strcmp($path,'admin/user/edit') === 0){
			return true;
		}else{
			return false;
		}
	}
	/*基本权限*/
	public function addBasicRule($id){
		
		if(isset($id)){
			Casbin::addPolicy($id, 'admin/index/*');
			return true;	
		}
	}
	/*文件上传,$type上传name,$callback返回数据/Json*/
	public function uploadFile($type='file',$from_id=0,$site=''){
			$files = request()->file();
			if(empty($files[$type])){
				return false;
			}else{
				$file = $files[$type];
				$sece = config('upload_file[$type]');
				$info = $file->validate($sece)->move('./uploads/' . $site);
				if($info){
					$this->reJson['msg']='上传成功';
					$this->reJson['data']= ['src'=>'/uploads/' . $site . '/' . $info->getSaveName(),'title'=>$info->getFilename()];
						$file_name = $info->getInfo();
						$data =[
							'from_id' => $from_id,
							'from_type' =>$file_name['type'],
							'from_site'	=>$site,
							'path'		=> $this->reJson['data']['src'],
							'ext'		=> json_encode(["ext_name"=>$info->getExtension(),"size"=>$file_name["size"]])
						];
						$res = Db::name('files')->insertGetId($data);
						if($res){
							$this->reJson['data']['id'] = $res;
						}	

				}else{
					$this->reJson['msg'] = $file->getError();
					$this->reJson['code'] = -1;
				}
			}
			return $this->reJson;
	}
	public function getTag($type = 1){
		$res = Db::name('tag')->where('type',$type)->select();
		return $res;
	}
}
